<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>手机商城</title>
<!-- CSS -->
<!--<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">-->
<link rel="stylesheet" href="{url:#css/materialize.min.css}" />
<link rel="stylesheet" href="{url:#css/me.css}" />
<link rel="stylesheet" href="{url:#css/iconfont.css}" />
<style type="text/css">
body{
	background:#FFFFFF;
}
a{
	color:black;
}
/**去除绿色下划线 - 开始*/
input::-webkit-input-placeholder {
    color: #7e7e7e;
    text-indent: 5px;
}
input:not([type]),input[type=text],input[type=password],input[type=email],input[type=url],input[type=time],input[type=date],input[type=datetime-local],input[type=tel],input[type=number],input[type=search],textarea.materialize-textarea{background-color:transparent;border:none;border-radius:0;outline:none;height:1.5em;width:100%;font-size:1rem;margin:0;padding:0;box-shadow:none;}
input:not([type]):focus:not([readonly]),input[type=text]:focus:not([readonly]),input[type=password]:focus:not([readonly]),input[type=email]:focus:not([readonly]),input[type=url]:focus:not([readonly]),input[type=time]:focus:not([readonly]),input[type=date]:focus:not([readonly]),input[type=datetime-local]:focus:not([readonly]),input[type=tel]:focus:not([readonly]),input[type=number]:focus:not([readonly]),input[type=search]:focus:not([readonly]),textarea.materialize-textarea:focus:not([readonly]){border-bottom:none;box-shadow:none}
/**去除绿色下划线 - 结束*/
</style>

<!-- JS -->
<script src="{url:#js/jquery-1.11.3.min.js}"></script>
<script src="{url:#js/materialize.min.js}"></script>
<script src="{url:#js/yang_check.js}"></script>
<script src="{url:#js/yang_check.js}"></script>

</head>
<body>
<!-- TitleBar 开始 -->
<div class="navbar-fixed">
  <nav>
    <div class="nav-wrapper red accent-4 valign-wrapper">
      <ul class="left">
        <li><a href="{url:/mobile/my_addr}"><i class="iconfont">&#xe63c;</i></a></li>
      </ul>
      <span class="title-bar-size center_align">{if:empty($id)}添加{else:}编辑{/if}地址</span>
      <ul class="right">
      	<!-- 
        <li><a><i class="iconfont">&#xe672;</i></a></li>
        -->
      </ul>
    </div>
  </nav>
</div>

<!-- TitleBar 结束 -->
<div class="addr-modify">
	<div class="addr-item">收货人:<input name="accept_name" id="accept_name" type="text" value="{$addr_obj['accept_name']|''}"/></div>
	<div class="divider"></div>
	<div class="addr-item">手机号码:<input name="mobile" id="mobile" type="text" value="{$addr_obj['mobile']|''}"/></div>
	<div class="divider"></div>
	<div class="addr-item">
		所在地区:
	</div>
	<div class="row">
		<div class="col s4">
			<select id="province" name="province" class="browser-default" onchange="change_city(this)">
			  <option value="0">选择省</option>
			  {query:name=area where=parent_id eq 0}
			  	  {if:!empty($id) && $addr_obj['province']==$item['id']}
			  	      <option value="{$item['id']}" selected>{$item['name']}</option>
			  	  {else:}
			  	  	  <option value="{$item['id']}">{$item['name']}</option>
			  	  {/if}
			      
			  {/query}
			</select>
		</div>
		<div class="col s4">
			<select id="city" name="city" class="browser-default" onchange="change_county(this)">
			  <option value="0">选择市</option>
			  {if:!empty($id)}
			      {list:items=$city_list}
			          {if:$addr_obj['city']==$item['id']}
			              <option value="{$item['id']}" selected>{$item['name']}</option>
			          {else:}
			              <option value="{$item['id']}">{$item['name']}</option>
			          {/if}
			          
			      {/list}
			  {/if}
			  
			</select>
		</div>
		<div class="col s4">
			<select id="county" name="county" class="browser-default">
			  <option value="0">选择县/区</option>
			  {if:!empty($id)}
			      {list:items=$county_list}
			          {if:$addr_obj['county']==$item['id']}
			              <option value="{$item['id']}" selected>{$item['name']}</option>
			          {else:}
			              <option value="{$item['id']}">{$item['name']}</option>
			          {/if}
			          
			      {/list}
			  {/if}
			</select>
		</div>
	</div>
	<div class="divider"></div>
	<div class="addr-item">地址邮编:<input name="zip" id="zip" type="text" placeholder='非必填' value="{$addr_obj['zip']}"/></div>
	<div class="divider"></div>
	<div class="addr-item">详细地址:<input name="addr" id="addr" type="text" value="{$addr_obj['addr']}"/></div>
	<div class="divider"></div>
</div>

	<div class="center clearfix" style="margin-right:10px; margin-left:10px; margin-top:20px;display:none;" id="div_warning">
		   警告！<span id="warning_msg"></span>
	</div>
						
	<div class="center clearfix" style="margin-right:10px; margin-left:10px; margin-top:20px;">
		<input type="hidden" id="id" name="id" value="{$id}"/>
		 
		<a class="waves-effect waves-light btn-large red z-depth-0 login_btn" href="javascript:;" onclick="save_addr(this)">保存地址</a>
	</div>
	
	
<script type="text/javascript">

function show_msg(msg)
{
	$("#warning_msg").html(msg);
	$("#div_warning").show();
	setTimeout(function(){$("#div_warning").hide();},1000);
}

function change_city(obj)
{
    var index=obj.selectedIndex;
    var sel_val = obj.options[index].value;
    if(sel_val == 0)
    {
        $("#city").html("<option value='0'>选择市</option>");
        $("#county").html("<option value='0'>选择县/区</option>");
    }
    else
    {
        $.post("{url:/mobile/getCityByProvince}",{'code':sel_val},function(result){
            $("#city").html(result);
            $("#county").html("<option value='0'>选择县/区</option>");
        });
    }

}


function change_county(obj)
{
    var index=obj.selectedIndex;
    var sel_val = obj.options[index].value;
    if(sel_val == 0)
    {
        //$("#city").html("<option value='0'>--市--</option>");
        $("#county").html("<option value='0'>选择县/区</option>");
    }
    else
    {
        $.post("{url:/mobile/getCountyByCity}",{'code':sel_val},function(result){
            $("#county").html(result);
        });
    }

}

/**
 * 保存收货地址
 */
function save_addr(obj)
{
	var id          = $("#id").val();
	var accept_name = $("#accept_name").val();
	var mobile      = $("#mobile").val();  //手机号码
	var province    = parseInt( $("#province").val() );//省
	var city        = parseInt( $("#city").val() );    //市
	var county      = parseInt( $("#county").val() );  //县
	var zip         = $("#zip").val();     //邮政编码
	var addr        = $("#addr").val();    //详细地址
	if( !input_is_null_validate('accept_name') )
	{
		show_msg('收货人不能为空');
		return;
	}
	
	if( !input_is_null_validate('mobile') )
	{
		show_msg('手机号码不能为空');
		return;
	}
	
	if( !input_pattern_validate('mobile','mobile') )
	{
		show_msg('您输入的手机号码不合法');
		return;
	}
	if( province == 0 || city == 0 || county == 0 )
	{
		show_msg('请选择完整的所在地');
		return;
	}

	if( !input_is_null_validate('addr') )
	{
		show_msg('详细地址不能为空');
		return;
	}

	//alert(id+" "+accept_name+" "+mobile+" "+province+" "+city+" "+zip+" "+addr);
	$(obj).removeAttr("onclick");

	
	
	$.post("{url:/mobile/save_addr}",{id:id,accept_name:accept_name,mobile:mobile,province:province,
		city:city,county:county,zip:zip,addr:addr},function(data){
		
		if(data['status'] == 'success')
	    {
			
		    yang_alert(data['msg']);
		    //个人中心页面
		    var is_choose_addr = "{$is_choose_addr}";

		    if(is_choose_addr == 1)
		    {
		    	document.location.href="{url:/mobile/my_addr}&is_choose_addr=1";
		    }
		    else
		    {
		    	window.location.replace("{url:/mobile/user_center}");
		    	document.location.href="{url:/mobile/my_addr}";   
		    }            
        }
        else
	    {
        	window.location.replace("{url:/mobile/user_center}");
            yang_alert(data['msg']);
            show_msg(data['msg']);
            document.location.href="{url:/mobile/my_addr}";
        }
    },'json');
	
}


</script>
</body>
</html>

